import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法八：使用CyclicBarrier
 */
public class ConcurrentTask8 {
    public String str;
    public CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        ConcurrentTask8 concurrentTask = new ConcurrentTask8();

        new Thread(concurrentTask::putStr).start();

        concurrentTask.cyclicBarrier.await();
        System.out.println(concurrentTask.getStr());

    }

    public void putStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            cyclicBarrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() {
        return str;
    }
}
